﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BartonExtension;
using Model;

namespace Backend
{
    public partial class FrmCatalog : DevExpress.XtraEditors.XtraForm
    {
        public DialogResult result { get; set; }
        public Catalog catalog { get; set; }
        public FrmCatalog()
        {
            InitializeComponent();
            this.result = DialogResult.Cancel;

            this.lstParent.Properties.DataSource = DataSourcePool.catalogs;
        }

        public FrmCatalog(Catalog c)
        {
            InitializeComponent();
            this.result = DialogResult.Cancel;

            catalog = c;
            this.lstParent.Properties.DataSource = DataSourcePool.catalogs;

            txtCode.Text = catalog.code;
            txtTitle.Text = catalog.title;
            txtTitleEn.Text = catalog.title_en;

            Catalog parent = DataSourcePool.catalogs.Find(k =>
                {
                    return k.code.Equals(catalog.parent_code) && k.layer == catalog.layer - 1;
                });
            lstParent.EditValue = parent == null ? -1 : parent.ID;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if(txtCode.Text.isNullOrEmpty())
            {
                XtraMessageBox.Show("Empty code is not allowed");
                return;
            }

            if(txtTitle.Text.isNullOrEmpty())
            {
                XtraMessageBox.Show("Empty title is not allowed");
                return;
            }

            if(txtTitleEn.Text.isNullOrEmpty())
            {
                XtraMessageBox.Show("Empty english title is not allowed");
                return;
            }

            int layer = -1;
            string p_code = "-1";
            if(lstParent.EditValue != null)
            {
                int selectedId = lstParent.EditValue.ToString().toInt32().Value;
                Catalog parent_catalog = DataSourcePool.catalogs.Find(x =>
                    {
                        return x.ID == selectedId;
                    });
                layer = parent_catalog == null ? 0 : parent_catalog.layer;
                p_code = parent_catalog == null ? "-1" : parent_catalog.code;
            }

            this.catalog = new Catalog()
            {
                ID = -1,
                code = this.txtCode.Text.ToUpper(),
                title = this.txtTitle.Text.ToUpper(),
                title_en = this.txtTitleEn.Text.ToUpper(),
                parent_code = p_code,
                layer = layer + 1
            };
            
            //Validate
            int validate = DataSourcePool.catalogs.FindIndex(x =>
                {
                    return x.code.Equals(catalog.code) && x.layer == catalog.layer;
                });
            if(validate != -1)
            {
                XtraMessageBox.Show("dumplicate catalog data");
                return;
            }

            this.result = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.result = DialogResult.Cancel;
            this.Close();
        }
    }
}